﻿using lBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;

namespace WebApplication160.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase, lUserBLL
    {
        private readonly lUserBLL db;

        public UserController(lUserBLL db , IWebHostEnvironment web)
        {
            this.db = db;
            Web = web;
        }

        public IWebHostEnvironment Web { get; }

        [HttpPost("AddAsync")]
        public Task<int> AddAsync(Userinfor userinfor)
        {
            return db.AddAsync(userinfor);
        }
        [HttpDelete("DeleteAsync")]
        public Task<int> DeleteAsync(int id)
        {
            return db.DeleteAsync(id);
        }
        [HttpGet("GetAllAsync")]
        public dynamic GetAllAsync(int pageindex, int pagesize)
        {
            return db.GetAllAsync(pageindex, pagesize);
        }
        [HttpPut("UpdateAsync")]
        public Task<int> UpdateAsync(Userinfor userinfor)
        {
            return db.UpdateAsync(userinfor);
        }
        [HttpPost("PostImg")]
        public dynamic PostImg(IFormFile file)
        {
            string a = DateTime.Now.ToString("yyyymmdd");
            string path = Web.WebRootPath + @"\Img\" + a +file.FileName;
            using (FileStream fs = System.IO.File.Create(path))
            {
                file.CopyTo(fs);
                fs.Close();
            }
            return "/Img/" + a + file.FileName;
        }
        [HttpGet("Fan")]
        public Userinfor Fan(int id)
        {
            return db.Fan(id);
        }
    }
}
